package de.justphil.tcg.tcgserver.game.mechanics.property.properties;

import java.util.List;

import de.justphil.tcg.tcgserver.game.mechanics.event.Event;
import de.justphil.tcg.tcgserver.game.mechanics.game.Attack;
import de.justphil.tcg.tcgserver.game.mechanics.game.Game;
import de.justphil.tcg.tcgserver.game.mechanics.monster.Monster;
import de.justphil.tcg.tcgserver.game.mechanics.monster.MonsterList;
import de.justphil.tcg.tcgserver.game.mechanics.property.Property;

public class Poisonous extends Property {

	public Poisonous(Game game) {

		super(game);
	}

	@Override
	protected MonsterList handleEvent(Event event) {

		MonsterList monsters;

		switch (event.getType()) {
		case Event.EVENT_ATTACK_INVOKED:

			monsters = new MonsterList();
			monsters.add(handleAttackInvoked((Attack) event.getData()));
			return monsters;

		case Event.EVENT_ATTACK_FINISHED:

			monsters = new MonsterList();
			monsters.add(handleAttackFinished((Attack) event.getData()));
			return monsters;

		default:

			assert false;
			break;
		}
		return null;
	}

	@Override
	public int getType() {

		return PROPERTY_POISONOUS;
	}

	@Override
	protected List<Integer> getListeningEventTypes() {

		List<Integer> types = super.getListeningEventTypes();
		types.add(Event.EVENT_ATTACK_INVOKED);
		types.add(Event.EVENT_ATTACK_FINISHED);

		return types;
	}

	private Monster handleAttackInvoked(Attack attack) {

		if (attack.getAttacker().equals(getMonster())) {

//			if (!(attack.getTarget()
//					.getProperties(Property.PROPERTY_POISON_RESISTANT).size() > 0)) {
//
				provideProperty(attack.getTarget(),
						PROPERTY_POISON_DEFENSE_LOSS);

				return attack.getTarget();
//			}
		}

		return null;
	}

	private Monster handleAttackFinished(Attack attack) {

		if (attack.getAttacker().equals(getMonster())) {

			withdrawProperties(attack.getTarget());
			return attack.getTarget();
		}

		return null;
	}
}
